<< define "user/edit.html" >>
<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>编辑用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <link rel="stylesheet" href="/static/css/mbgadmin.css">
    <script src="/static/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
    <script type="text/javascript" src="/static/js/mbgadmin.js"></script>
</head>
<body>
<form class="layui-form" lay-filter="users_edit_form">
    <div class="layui-form-item">
        <label class="layui-form-label">用户名<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" placeholder="请输入用户名" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机</label>
        <div class="layui-input-block">
            <input type="text" name="phone" placeholder="请输入手机号" autocomplete="off" class="layui-input"
                   lay-verify="phone">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" placeholder="请输入邮箱" autocomplete="off" class="layui-input"
                   lay-verify="email">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="1" title="男" checked id="maleRadio">
            <input type="radio" name="sex" value="0" title="女" id="femaleRadio">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">状态<span class="x-red">*</span></label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="启用" checked>
            <input type="radio" name="status" value="0" title="禁用">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">角色</label>
        <div class="layui-input-block" id="role"></div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="addAdmin" id="submit">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>

<script>
    function child(dataFromFather) {
        layui.use(['form', 'transfer', 'jquery'], function () {
            var form = layui.form;
            let $ = layui.jquery;
            let transfer = layui.transfer;
            form.val('users_edit_form', {
                "name": dataFromFather.name,
                "phone": dataFromFather.phone,
                "email":dataFromFather.email
            });
            let value =[]
            layui.each(dataFromFather.roles,function (k,v) {
                value.push(v.ID)
            })
            $.get("/users/getRole", function (data) {
                // 如果是超级管理员账号就不允许把超级管理员角色给去除
                if (dataFromFather.ID===1){
                    for (let i=0;i<data.length; i++) {
                        if (data[i].value==='1'){
                            $.extend(data[i], {
                                "disabled": true
                            });
                            break;
                        }
                    }
                }
                transfer.render({
                    elem: '#role',
                    id: 'role',
                    showSearch:true,
                    title: ['角色列表', '已选中角色列表'],
                    data: data,
                    value:value,
                });
            })

            form.on('submit(addAdmin)', function (data) {
                $("#submit").addClass("layui-btn-disabled") //防止连续点击添加键。
                let loadIndex = layer.load(2);
                var getData = transfer.getData('role');
                const rolesValues = getData.map(role => role.value).join(',');
                data.field.roles = rolesValues
                $.post("/users/edit/"+dataFromFather.ID, data.field, function (result) {
                    if (result.code === 0) {
                        layer.close(loadIndex)
                        parent.layer.msg(result.msg);
                        ReloadLists()
                    } else {
                        $("#submit").removeClass("layui-btn-disabled")
                        layer.close(loadIndex)
                        parent.layer.msg(result.msg);
                    }
                });
                return false;
            });
        });
    }
</script>

</body>
</html>
<< end >>